package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;
/*
数据库users.db，数据库表user
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    /**
     * @param
     *      userId：用户ID
     * @param
     *      username :用户名
     * @param
     *      password  :密码
     *  @param
     *      sex  :性别
     * */
public  static final  String user="create table user(userId integer primary key autoincrement,username varchar(40),password varchar(40))";
    public  static final  String userBackups  ="create table userBackups(userId integer primary key autoincrement,username varchar(40),password varchar(40))";
private Context mcontext;
    public DBOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
       this.mcontext=context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(user);
        db.execSQL(userBackups);
        Cursor cursor=db.query("user",null,null,null,null,null,null);
        ContentValues values = new ContentValues();
        while (cursor.moveToNext()){
            String username = cursor.getString(cursor.getColumnIndex("username"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            values.put("username",username);
            values.put("password",password);
            db.insert("userBackups",null,values);
            values.clear();
        }
        cursor.close();



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL("ALTER TABLE user ADD sex varchar(20) ");

        }
    }
}
